/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package runestoneserver;
//import lejos.nxt.*;
import java.awt.event.*;
import lejos.nxt.*;
import lejos.pc.comm.*;
import java.io.*;
import javax.swing.*;


/**
 *
 * @author Fredrik
 */
public class Server {
    private BlueT bt = new BlueT();
    private int prevcode;
    public Server() {
        bt.connect();
        this.gui();
        //bt.send("forward\n");
        //System.out.println("forward");

        //Make instances of different connections. Bluetooth, TCP and UDP (or RTP).
        //this.bt = new BT();
        //Connection tcp = new TCP(); //Opening TCP socket.
        //Connection udp = new UDP(); //Opening UDP socket.

        /**
         * Basic algorithm:
         * 1. Stream image data from the usb using the UDP connection.
         * 2. Listen for incoming messages from the TCP socket.
         * 3. Send command data from step 2 to the Bluetooth -> robot.
         * 4. Wait for confirmation from robot.
         * 4.1 if no confirmation received send debug on tcp to gui. Increase lost command by 1.
         * 4.2 if confirmation received send success debug message on tcp to gui.
         * 5. Go back to step 1.
         */
            
    }
    
    public void gui() {
        JFrame frame = new JFrame();
        frame.add(buildGUI());
        frame.pack();    
        frame.setVisible(true);
        frame.addKeyListener(listener);
    }
    
    public JPanel buildGUI() {
        JPanel panel = new JPanel();
        JButton up = new JButton();
        panel.add(up);
        up.addKeyListener(listener);
        up.setVisible(false);
        return panel;
    }
    
    KeyListener listener = new KeyListener() {
        
            public void keyTyped(KeyEvent e) {
                
            }

            public void keyPressed(KeyEvent e) {
                
                int code = e.getKeyCode();
                //System.out.println(code);
                //String button = e.getKeyText(code);
                if(code != prevcode) { 
                    prevcode = code;
                    switch(code) {
                        case 38:
                            System.out.println("forward pressed");
                            bt.send("forward\n");
                            break;
                        case 40:
                            System.out.println("backward pressed");
                            bt.send("backward\n");
                            break;
                        case 37:
                            System.out.println("left pressed");
                            bt.send("left\n");
                            break;
                        case 39:
                            System.out.println("right pressed");
                            bt.send("right\n");
                            break;
                        default:
                            break;

                    }
                }
            }

            public void keyReleased(KeyEvent e) {
                System.out.println("Key Released");
                prevcode = 0;
                bt.send("stop\n");
            }
            /*
            public void location(int location) {
                if(KeyEvent.KEY_PRESSED) {
                    this.keyPressed(e);
                }
                    
            }*/
    };

    public static void main(String[] args) {
        new Server();
    }

}
